// pages2/pages/person/bindAccount/index.js
const $api = require('../../../../request/api.js').API;
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
import { USER_ID } from '../../../../utils/storage';
import { selectImg, validate } from '../../../../utils/util';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ymAccountId: '',
    otherYmAccountId: '',
    fullName: '',
    phone: '',
    idCard: '',
    bankCode: '',
    bankName: '',
    branchCode: '',
    branchName: '',
    cardNo: '',
    cardType: '',
    bankCardFront: '',
    bankCardFrontPath: '',
    bankCardBack: '',
    bankCardBackPath: '',
    idCardFront: '',
    idCardFrontPath: '',
    idCardBack: '',
    idCardBackPath: '',
    defaultFlag: '',
    openState: '',
    relationState: '',
    showBankPopup: false,
    showPhonePopup: false,
    inputPhone: '',
    code: '',
    bankList: [],
    second: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.setData({
      userId: wx.getStorageSync(USER_ID)
    });
    await this.getYmAccount();
    if (!this.data.ymAccountId) {
      await this.getAccountInfo();
    }

    this.getBankList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  async getYmAccount() {
    const params = {
      userId: this.data.userId,
      userType: 3
    };
    const res = await $api.getYmAccount(params);
    const data = res.data.data;
    if (!data) return;
    const {
      fullName,
      phone,
      idCard,
      bankCode,
      bankName,
      branchCode,
      branchName,
      cardNo,
      bankCardFront,
      bankCardBack,
      idCardFront,
      idCardBack,
      ymAccountId,
      otherYmAccountId,
      openState,
      relationState
    } = data;
    this.setData({
      fullName,
      phone,
      idCard,
      bankCode,
      bankName,
      branchCode,
      branchName,
      cardNo,
      bankCardFront,
      bankCardFrontPath: bankCardFront,
      bankCardBack,
      bankCardBackPath: bankCardBack,
      idCardFront,
      idCardFrontPath: idCardFront,
      idCardBack,
      idCardBackPath: idCardBack,
      ymAccountId,
      otherYmAccountId,
      openState,
      relationState
    });
  },
  async getAccountInfo() {
    const res = await $api.accountInfo({ userId: this.data.userId });
    const data = res.data.data;
    if (!data) return;
    const { fullName, phone, idCard, idCardFront, idCardBack } = data;
    this.setData({
      fullName,
      phone,
      idCard,
      idCardFront,
      idCardBack,
      idCardFrontPath: idCardFront,
      idCardBackPath: idCardBack
    });
  },
  // 获取银行列表
  async getBankList() {
    const res = await $api.listBankCode();
    this.setData({
      bankList: res.data.data || []
    });
  },
  // 显示手机号输入
  handleInputPhone() {
    this.setData({
      showPhonePopup: true
    });
  },
  // 选择银行
  handleSelectBank() {
    this.setData({
      showBankPopup: true
    });
  },
  // 去支行列表
  toBranchBank() {
    wx.navigateTo({
      url: '../bankBranchList/index?bankCode=' + this.data.bankCode
    });
  },
  // 确定选择银行
  handleBankConfirm(e) {
    const { bankCode, bankName } = e.detail.value;
    this.setData({
      bankCode,
      bankName
    });
    this.handleBankClose();
  },
  // 取消选择银行
  handleBankClose(e) {
    this.setData({
      showBankPopup: false
    });
  },
  /**
   *  输入
   */
  handleInput(e) {
    const { prop } = e.currentTarget.dataset;
    this.setData({
      [prop]: e.detail
    });
  },
  // 获取验证码
  async getCode() {
    const { inputPhone } = this.data;
    const rules = [
      {
        key: 'mobile',
        value: '请输入手机号',
        pattern: [{ reg: /(^1(3|4|5|6|7|8|9)\d{9}$)|(^0\d{2,3}-?\d{7,8}$)/, value: '手机号格式错误' }]
      }
    ];
    const params = {
      mobile: inputPhone,
      smsType: 103,
      useType: 1
    };
    if (!validate(rules, params)) return;
    const res = await $api.getPhoneCode(params);
    // .then(res => {
    console.log(res);
    if (res.data.status == 0) {
      wx.showToast({
        title: '发送验证码成功',
        status: 'success'
      });
      this.setData({
        second: 60
      });
      this.timer = setInterval(() => {
        this.setData({
          second: this.data.second - 1
        });
        if (this.data.second == 0) {
          clearInterval(this.timer);
        }
      }, 1000);
    }
    // });
  },
  // 获取翼码信息
  async handleGetInfo() {
    const { inputPhone, code } = this.data;
    const rules = [
      {
        key: 'phone',
        msg: '请输入手机号',
        pattern: [{ reg: /(^1(3|4|5|6|7|8|9)\d{9}$)|(^0\d{2,3}-?\d{7,8}$)/, value: '手机号格式错误' }]
      },
      {
        key: 'code',
        msg: '请输入验证码'
      }
    ];
    const params = {
      phone: inputPhone,
      code
    };
    if (!validate(rules, params)) return;
    const res = await $api.getDataByNameType(params);
    const data = res.data.data;
    if (!data) {
      Toast.fail('暂无翼码账户');

      return;
    } else {
      const {
        fullName,
        phone,
        idCard,
        bankCode,
        bankName,
        branchCode,
        branchName,
        cardNo,
        bankCardFront,
        bankCardBack,
        idCardFront,
        idCardBack,
        ymAccountId,
        otherYmAccountId
      } = data;
      this.setData({
        fullName,
        phone,
        idCard,
        bankCode,
        bankName,
        branchCode,
        branchName,
        cardNo,
        bankCardFront,
        bankCardFrontPath: bankCardFront,
        bankCardBack,
        bankCardBackPath: bankCardFront,
        idCardFront,
        idCardFrontPath: idCardFront,
        idCardBack,
        idCardBackPath: idCardBack,
        ymAccountId,
        otherYmAccountId
      });
    }
    this.handlePhoneClose();
  },
  // 关闭手机号获取翼码信息
  handlePhoneClose() {
    this.setData({
      showPhonePopup: false
    });
  },
  /**
   *删除
   */
  handleDelete(e) {
    const { prop } = e.currentTarget.dataset;
    this.setData({
      [prop]: ''
    });
  },
  async handleChooseImg(e) {
    const { prop } = e.currentTarget.dataset;
    console.log(prop);
    const { tempPath, baseCode } = await selectImg();
    const path = prop + 'Path';
    console.log();
    this.setData({
      [prop]: baseCode,
      [path]: tempPath
    });
  },
  async submit() {
    const rules = [
      {
        key: 'fullName',
        msg: '请输入姓名'
      },
      {
        key: 'phone',
        msg: '请输入手机号',
        pattern: [{ reg: /(^1(3|4|5|6|7|8|9)\d{9}$)|(^0\d{2,3}-?\d{7,8}$)/, msg: '手机号格式错误' }]
      },
      {
        key: 'idCard',
        msg: '请输入身份证号',
        pattern: [{ reg: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, msg: '身份证格式错误' }]
      },
      {
        key: 'bankCode',
        msg: '请选择银行'
      },
      {
        key: 'branchCode',
        msg: '请选择银行支行'
      },
      {
        key: 'cardNo',
        msg: '请输入银行卡号'
      }
      // {
      //   key: 'idCardFront',
      //   msg: '请上传身份证正面照'
      // },
      // {
      //   key: 'idCardBack',
      //   msg: '请上传身份证反面照'
      // },
      // {
      //   key: 'bankCardFront',
      //   msg: '请上传银行卡正面照'
      // },
      // {
      //   key: 'bankCardBack',
      //   msg: '请上传银行卡反面照'
      // },
    ];
    if (!validate(rules, this.data)) return;
    await Dialog.confirm({
      title: '提示',
      message: '确定提交吗？'
    });

    const {
      fullName,
      phone,
      idCard,
      cardNo,
      bankCode,
      branchCode,
      bankName,
      branchName,
      bankCardFront,
      bankCardBack,
      idCardFront,
      idCardBack,
      ymAccountId,
      otherYmAccountId,
      userId
    } = this.data;
    const params = {
      userId,
      fullName,
      phone,
      idCard,
      bankCode,
      bankName,
      branchCode,
      branchName,
      bankCardFront,
      bankCardBack,
      idCardFront,
      idCardBack,
      cardName: fullName,
      cardNo,
      cardType: 2,
      defaultFlag: 1,
      userType: 3
    };
    if (ymAccountId) params.ymAccountId = ymAccountId;
    if (otherYmAccountId) params.otherYmAccountId = otherYmAccountId;
    Toast.loading({
      message: '提交中...',
      duration: 0,
      mask: true,
      forbidClick: true
    });
    await $api.setYmAccount(params);
    Toast.success({
      message: '提交成功，请等待审核',
      forbidClick: true
    });
    setTimeout(() => {
      wx.switchTab({
        url: '/pages/person/index'
      });
    }, 1500);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
